<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>员工出勤签到明细</title>
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../js/jquery-3.1.1.min.js"></script>
    <style>
        body{margin: 10px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
</head>
<body>
<div>
    <div>
        <form>
            <div class="layui-inline">
                <label class="layui-form-label">员工姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="staffName" lay-verify="title" autocomplete="off" placeholder="请输入员工姓名"
                           class="layui-input" id="staffName">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">日期</label>
                <div class="layui-input-block">
                    <input type="text" name="dates" lay-verify="title" autocomplete="off" placeholder="请输入日期"
                           class="layui-input" id="dates">
                </div>
            </div>
            <input type="button" class="layui-btn layui-btn-sm" lay-event="select" id="select" value="查询"></input>
        </form>
    </div>
    <table class="layui-hide" id="demo" lay-filter="test"></table>
</div>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../layui/layui.js"></script>
<script>
    var table;
    layui.config({
        version: '1615206508080' //为了更新 js 缓存，可忽略
    });

    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function(){
        var laydate = layui.laydate //日期
            ,laypage = layui.laypage //分页
            ,layer = layui.layer //弹层
            ,carousel = layui.carousel //轮播
            ,upload = layui.upload //上传
            ,element = layui.element //元素操作
            ,slider = layui.slider; //滑块
            table = layui.table //表格


        //监听Tab切换
        element.on('tab(demo)', function(data){
            layer.tips('切换了 '+ data.index +'：'+ this.innerHTML, this, {
                tips: 1
            });
        });

        //执行一个 table 实例
        table.render({
            elem: '#demo'
            , id: 'logisticsGate'
            ,height: 700
            ,url: '/logisticsGate/select' //数据接口
            ,title: '出勤表'
            ,page: true //开启分页
            ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,totalRow: true //开启合计行
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'gateId', title: 'ID', width:80, sort: true, fixed: 'left', totalRowText: '合计：'}
                ,{field: 'staffName', title: '员工编号', width:80}
                ,{field: 'gateDates', title: '签到日期', width: 120}
                ,{field: 'gateBegin', title: '上班时间', width: 150}
                ,{field: 'gateOut', title: '下班时间', width:150}
                ,{field: 'gateDate', title: '上班时长', width: 100, sort: true, totalRow: true}
                ,{field: 'gateRemark', title: '出勤备注', width:150}
                ,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemo'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        let gateId = "";
                        for (let i = 0; i < data.length; i++) {
                            gateId += data[i].gateId + ",";
                        }
                        gateId= gateId.substr(0, gateId.length - 1);
                        let url = "/logisticsGate/batch";
                        let data_ = {gateId:gateId};
                        console.info(data_)
                        $.post(url, data_, function (map) {
                            if (map.state == 1) {
                                table.reload('logisticsGate')
                                layer.msg(map.msg);
                            } else {
                                layer.msg(map.msg)
                            }
                        }, "json")
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
             if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    let url="/logisticsGate/delete";
                    let data_={gateId:data.gateId};
                    $.post(url, data_,function (map){
                        if (map.state==0){
                            layer.close(map.message());
                        }
                    },"json")
                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){
                 console.info(data.gateId);
                layer.open({
                    type: 2,
                    area: ['500px', '500px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/checking-in/signForm.html?gateId='+data.gateId,
                  success: function (layero, index) {

                  }

                });
            }
        });
    });

    $(function (){
        $("#select").on("click",function (){
            var data_={
                staffName: $("#staffName").val(),
                dates:$("#dates").val(),
            };
            console.info(data_);
            //执行重载
            table.reload('logisticsGate', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where:data_
            });
        })
    })
</script>
</body>
</html>
